﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using System.Data;
using System.Configuration;

namespace WaveMaster.Holidays.Club.Web.AspxPage
{
    public partial class ReportViewer : System.Web.UI.Page
    {
        ReportDocument rd;
        protected void Page_Load(object sender, EventArgs e)
        {
            string p = Request.QueryString["ReportFile"];
            string c = Request.QueryString["ReportDataSource"];
            if (p == "" && c == "")
                return;


            string sessionReportFile = p;
            DataSet sessionReportData = new DataSet();
            sessionReportData.ReadXml(ConfigurationManager.AppSettings["ReportDataTemp"] + c);


           // string sessionReportFile = Session[Request.QueryString["ReportFile"]].ToString();
           // DataSet sessionReportData = (DataSet)Session[Request.QueryString["ReportDataSource"]];
            
            if (sessionReportData != null)
            {
                string repPath = ConfigurationManager.AppSettings["ReportPath"] + sessionReportFile;
                DataSet dsRD = sessionReportData;
                rd = new ReportDocument();
                rd.Load(repPath);
                rd.SetDataSource(dsRD.Tables[0]);
               
                //rd.PrintOptions.PrinterName = "Snagit 10 on 172.25.67.153";
                //rd.PrintToPrinter(1, false, 1, 1);
                if ((sessionReportFile == "ReportManifestByTripSG.rpt") || (sessionReportFile == "ReportManifestByTripID.rpt"))
                {
                    rd.DataDefinition.FormulaFields["vessel"].Text = "'" + sessionReportData.Tables["Header"].Rows[0].ItemArray[2].ToString() + "'";
                    rd.DataDefinition.FormulaFields["code"].Text = "'" + sessionReportData.Tables["Header"].Rows[0].ItemArray[0].ToString() + "'";
                    rd.DataDefinition.FormulaFields["deptime"].Text = "'" + Convert.ToDateTime(sessionReportData.Tables["Header"].Rows[0].ItemArray[1]).ToString("HH:mm") + "'";
                    rd.DataDefinition.FormulaFields["depdate"].Text = "'" + Convert.ToDateTime(sessionReportData.Tables["Header"].Rows[0].ItemArray[1]).ToString("dd-MMM-yyyy") + "'";
                    rd.DataDefinition.FormulaFields["depart"].Text = "'" + sessionReportData.Tables["Header"].Rows[0].ItemArray[3].ToString() + "'";
                    rd.DataDefinition.FormulaFields["arrive"].Text = "'" + sessionReportData.Tables["Header"].Rows[0].ItemArray[4].ToString() + "'";
                    rd.DataDefinition.FormulaFields["travelagent"].Text = "'" + sessionReportData.Tables["Header"].Rows[0].ItemArray[5].ToString() + "'";
                }
                CrystalReportViewer.ReportSource = rd;
                CrystalReportViewer.DataBind();
            }
        }

        protected void CrystalReportViewer_Unload(object sender, EventArgs e)
        {
            rd.Clone();
            rd.Close();
            rd.Dispose();
        }
    }
}